package com.cn.pxxy.controller.impl;

import com.cn.pxxy.controller.UserController;
import com.cn.pxxy.entity.User;
import com.cn.pxxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserControllerImpl implements UserController {

    @Autowired
    private UserService userService;


    public List selectAll() {
        return null;
    }

    public void delete() {

    }

    public User selectById(int id) {
        User user = userService.selectById(id);
        System.out.println(user);

        return user;
    }

    public void adduser() {

    }

    public void update(int id) {

    }

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request, HttpServletResponse response){
        String password = user.getPassword();  //记录未加密的密码
        user = userService.selectLogin(user.getUsername(), password);
        if ( user.getPassword().equals(password) ) {
            System.out.println("success");
            return "redirect:../index.html";
        } else {
            request.setAttribute("msg", "登录失败,用户名或密码错误");

            System.out.println("successPPPPPP");
            return "redirect:../login.html";  //转发到登录页面
        }
    }
}
